﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyDoiXe.lop_doi_tuong;
using QuanLyDoiXe.Lop_du_lieu;
using System.IO;
using BarcodeLib;

namespace QuanLyDoiXe
{
    public partial class frmquangduongxechay : Form
    {
        SqlConnection conn = new SqlConnection("Data Source=ACER;Initial Catalog=quanlydoixevinamilk;Integrated Security=True");
        public frmquangduongxechay()
        {
            InitializeComponent();
        }
        #region 1. Thoát & Nhập lại
        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnnhaplai_Click(object sender, EventArgs e)
        {
            txtbienso.Text = "";
            txtngaychay.Text = "";
            txtsokmchay.Text = "";
            txttenduong.Text = "";
            txtthoigiandung.Text = "";
            txtxangtieuthu.Text = "";
        }
        #endregion
        #region 2. Hiển Thị BCQDXC
        ListViewItem item;
        private void HienThiBCQDXC()
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("Select * from bcquangduongxechay,xecongty where bcquangduongxechay.Mxe = xecongty.Mxe ", conn);
                SqlDataReader sqlDA = cmd.ExecuteReader();
                int i = 0;
                lvbcquangduongxechay.View = View.Details;
                lvbcquangduongxechay.FullRowSelect = true;
                while (sqlDA.Read())
                {
                    i++;
                    item = new ListViewItem(i.ToString());
                    item.SubItems.Add(sqlDA["Bienso"].ToString());
                    item.SubItems.Add(sqlDA["Tenduong"].ToString());
                    item.SubItems.Add(sqlDA["Sokmchay"].ToString());
                    item.SubItems.Add(sqlDA["Xangtieuthu"].ToString());
                    item.SubItems.Add(sqlDA["Thoigiandung"].ToString());
                    item.SubItems.Add(sqlDA["Ngaychay"].ToString());
                    lvbcquangduongxechay.Items.Add(item);
                }
                cmd.Dispose();
                sqlDA.Close();
                conn.Close();
            }
            catch
            {
            }
        }

        private void lvbcquangduongxechay_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem item = e.Item;
            txtbienso.Text = item.SubItems[1].Text;
            txttenduong.Text = item.SubItems[2].Text;
            txtsokmchay.Text = item.SubItems[3].Text;
            txtxangtieuthu.Text = item.SubItems[4].Text;
            txtthoigiandung.Text = item.SubItems[5].Text;
            txtngaychay.Text = item.SubItems[6].Text;
        }
        string Bienso, Tenduong;
        int Sokmchay, Xangtieuthu, Thoigiandung;
        DateTime Ngaychay;
        private void lvbcquangduongxechay_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lvbcquangduongxechay.Items.Count; i++)
            {
                if (lvbcquangduongxechay.Items[i].Selected)
                {
                    Bienso = item.SubItems[1].Text;
                    Tenduong = item.SubItems[2].Text;
                    Sokmchay =Convert.ToInt32(item.SubItems[3].Text);
                    Xangtieuthu =Convert.ToInt32(item.SubItems[4].Text);
                    Thoigiandung = Convert.ToInt32(item.SubItems[5].Text);
                    Ngaychay =Convert.ToDateTime(item.SubItems[6].Text);
                }
            }
        }

        private void frmquangduongxechay_Load(object sender, EventArgs e)
        {
            HienThiBCQDXC();
        }
        #endregion
        #region 3. cập nhập
        private void btncapnhap_Click(object sender, EventArgs e)
        {
            string strBienso = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select Mxe from xecongty where Bienso=N\'" + txtbienso.Text + "\'");
            while (dr.Read())
            {
                strBienso = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();

            lop_ket_noi.huyketnoi();
            if ( txtbienso.Text != "" && txttenduong.Text != "" && txtsokmchay.Text != "" && txtxangtieuthu.Text != "" && txtthoigiandung.Text != "" && txtngaychay.Text != "" )
            {
                lop_bcquangduongxechay quangduong = new lop_bcquangduongxechay(strBienso, txttenduong.Text, Convert.ToInt32(txtsokmchay.Text), Convert.ToInt32(txtxangtieuthu.Text), Convert.ToInt32(txtthoigiandung.Text), Convert.ToDateTime(txtngaychay.Text));
                if (quangduong.capnhat())
                    MessageBox.Show("Quá trình cập nhật báo cáo thành công!");
                else
                    MessageBox.Show("Quá trình cập nhật báo cáo thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ thông tin báo cáo! ");
            lvbcquangduongxechay.Items.Clear();
            HienThiBCQDXC();
        }
        #endregion
        #region 4. in bao cao
        public static string BS = "";
        public static string TD = "";
        public static string Sokm = "";
        public static string Xtieuthu = "";
        public static string TGdung = "";
        public static string Ngayxechay = "";
        private void btnxuat_Click(object sender, EventArgs e)
        {
            if (txtbienso.Text != "" && txttenduong.Text != "" && txtsokmchay.Text != "" && txtxangtieuthu.Text != "" && txtthoigiandung.Text != "" && txtngaychay.Text != "")
            {
                BS = txtbienso.Text;
                TD = txttenduong.Text;
                Sokm = Convert.ToString(txtsokmchay.Text);
                Xtieuthu = Convert.ToString(txtxangtieuthu.Text);
                TGdung = Convert.ToString(txtthoigiandung.Text);
                Ngayxechay = Convert.ToString(txtngaychay.Text);

                frminbaocaoquangduong frmInbacao = new frminbaocaoquangduong();
                frmInbacao.Show();
            }
            else
            { }
        }
        #endregion
    }
}
